package com.example.laixunlong.tools;

import android.app.Activity;
import android.os.Environment;
import android.util.Log;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.Buffer;

public class FileInOutPut {
/*路径获取=======================================================================================*/

    /*获取当前应用所在路径     /storage/emulated/0/Android/data/com.example.laixunlong/files/Download/filename.xxxx       */
    public static String getAppPath(Activity a,String filename)
    {
        String p= a.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).toString();
        String pa=p+ File.separatorChar+filename;
        return pa;
    }
    /*获取设备公共路径    /storage/emulated/0/Download/filename.xxxx        */
    public static String getPublicPath(Activity a,String filename)
    {
        String p=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString();
        String pa=p+File.separatorChar+filename;
        return pa;
    }
    /*获取应用私有路径,不允许外部应用访问,该路径无法通过设备查看,只能通过代码进行增删改查
         /data/user/0/com.example.laixunlong/files/filename.xxxx    */
    public static String getPrivatePath(Activity a,String filename)
    {
        String p=a.getFilesDir().toString();
        String pa=p+File.separatorChar+filename;
        return pa;
    }


/*保存txt文件===================================================================================*/
    public static void saveTXT(String path,String txt)
    {
        BufferedWriter a=null;
        try {
            a=new BufferedWriter(new FileWriter(path));
            a.write(txt);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            if (a!=null)
            {
                try {
                    a.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
/*读取txt文件===================================================================================*/
    public static String getTXT(String path)
    {
        BufferedReader a=null;
        StringBuilder s=new StringBuilder();
        try {
            a=new BufferedReader(new FileReader(path));
            String list=null;
            while((list=a.readLine()) != null)
            {
                s.append(list+"\n");
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            if (a!=null)
            {
                try {
                    a.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            else
            {
                return "";
            }
        }
        return s.toString();
    }
/*删除文件===================================================================================*/
    public static void deleteFile(String path)
    {
        File file = new File(path);
        // 检查文件是否存在以及是否是txt文件
        if (file.exists() && path.endsWith(".txt")) {
            boolean deleted = file.delete();
            if (deleted) {
                Log.e("结果","删除成功");// 文件删除成功
            } else {
                Log.e("结果","删除失败");// 文件删除失败
            }
        }
    }
}
